package cn.learn.office.excel_v2.sort;


import cn.learn.office.excel_v2.ExportExcelUtil;
import cn.learn.office.excel_v2.ExportExcelVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 处理有序的linkedHashMap集合的排序方法
 *
 * @author huangyezhan
 * @version 2.0
 * @date 2020年4月20日21:18:15
 */
@Deprecated
public class LinkedHashMapSort implements HandlerColumnSort {

    /**
     * 处理有序的map
     *
     * @param excelVo             Excel表格存储的数据
     * @param createStyleInstance poi的cell样式对象
     * @param sheet               poi的sheet页对象
     */
    @Override
    public void columnSort(ExportExcelVo excelVo, Sheet sheet, Map<ExportExcelVo.Position, CellStyle> createStyleInstance) {
        List columns = excelVo.getList();
        //开始导入数据开始行数号（去掉表头的行数）
        int startRowNum = excelVo.getHeadRowNum();
        //行数
        int      i        = 0;
        Iterator iterator = columns.iterator();
        //迭代出行数
        Row  row  = null;
        Cell cell = null;
        while (iterator.hasNext()) {
            row = sheet.createRow(i + startRowNum);
            //列数
            int                            j       = 0;
            Map<String, Object>            next    = (Map<String, Object>) iterator.next();
            Set<Map.Entry<String, Object>> entries = next.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                Object o = entry.getValue();
                //超出预设长度的不要了
                if (j >= excelVo.getColumnNum()) {
                    break;
                }
                cell = row.createCell(j);
                ExportExcelUtil.fillDatabyRealType(createStyleInstance, o, cell);
                j++;
            }
            i++;
        }
    }

}
